<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="banner图状态" prop="bannerState">
        <el-select v-model="queryParams.bannerState" placeholder="请选择" clearable size="small" >
          <el-option label="请选择" value="" />
          <el-option label="已展示" value="0" />
          <el-option label="未展示" value="1" />


        </el-select>
      </el-form-item>
      <el-form-item label="视频id" prop="videoId">
        <el-select v-model="queryParams.videoId" clearable filterable placeholder="请选择"
                   v-el-select-loadmore="loadmore" >
          <el-option
            v-for="item in options"
            :key="item.spVideo.videoId"
            :label="item.spVideo.videoName"
            :value="item.spVideo.videoId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xqsp:banner:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['xqsp:banner:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xqsp:banner:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xqsp:banner:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="spBanner.bannerId" />
      <el-table-column label="图片地址" align="center" prop="spBanner.bannerImg" />
      <el-table-column label="banner图状态" align="center" prop="spBanner.bannerState">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.spBanner.bannerState"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)">

          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="视频id" align="center" prop="videoName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xqsp:banner:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xqsp:banner:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改banner图操作对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片地址" prop="bannerImg">
          <el-input v-model="form.bannerImg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="banner图状态" prop="bannerState">
          <el-input v-model="form.bannerState" placeholder="请输入banner图状态" />
        </el-form-item>
        <el-form-item label="视频id" prop="videoId">
          <el-select v-model="form.videoId" clearable filterable placeholder="请选择"
                     v-el-select-loadmore="loadmore" >
            <el-option
              v-for="item in options"
              :key="item.spVideo.videoId"
              :label="item.spVideo.videoName"
              :value="item.spVideo.videoId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBanner, getBanner, delBanner, addBanner, updateBanner, exportBanner,changeBannerStatus } from "@/api/xqsp/banner";
import { listVideo} from "@/api/xqsp/video";
export default {
  directives: {
    'el-select-loadmore': {
      bind(el, binding) {
        // 获取element-ui定义好的scroll盒子
        const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap');
        SELECTWRAP_DOM.addEventListener('scroll', function () {
          /**
           * scrollHeight 获取元素内容高度(只读)
           * scrollTop 获取或者设置元素的偏移值,常用于, 计算滚动条的位置, 当一个元素的容器没有产生垂直方向的滚动条, 那它的scrollTop的值默认为0.
           * clientHeight 读取元素的可见高度(只读)
           * 如果元素滚动到底, 下面等式返回true, 没有则返回false:
           * ele.scrollHeight - ele.scrollTop === ele.clientHeight;
           */
          const condition = this.scrollHeight - this.scrollTop <= this.clientHeight;
          if (condition) {
            binding.value();
          }
        });
      }
    }
  },
  name: "Banner",
  components: {
  },

  data() {
    return {
      formData: {
        pageNum: 1,
        pageSize: 10,
      },
      //视频下拉框
      options:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // banner图操作表格数据
      bannerList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bannerImg: null,
        bannerState: null,
        videoId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  mounted() {
    this.getVideoList();
  },

  created() {
    this.getList();

  },
  methods: {
    loadmore() {
      this.formData.pageNum++;
      this.getVideoList();
    },

    //查询视频
    getVideoList() {
      listVideo(this.formData).then(response => {
        if (this.options.length<response.total){
          this.options =  [...this.options, ...response.rows]
        }


      });
    },



    /** 查询banner图操作列表 */
    getList() {
      this.loading = true;
      listBanner(this.queryParams).then(response => {
        this.bannerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bannerId: null,
        bannerImg: null,
        bannerState: null,
        videoId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bannerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加banner图操作";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bannerId = row.spBanner.bannerId || this.ids
      getBanner(bannerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改banner图操作";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bannerId != null) {
            updateBanner(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBanner(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bannerIds = row.spBanner.bannerId || this.ids;
      this.$confirm('是否确认删除banner图操作编号为"' + bannerIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBanner(bannerIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有banner图操作数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBanner(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    // 是否展示
    handleStatusChange(row) {
      let text = row.spBanner.bannerState === 0 ? "展示" : "不展示";
      this.$confirm('确认要"' + text + '"编号为"' + row.spBanner.bannerId + '"的banner图吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changeBannerStatus(row.spBanner.bannerId, row.spBanner.bannerState);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function() {
        row.spBanner.bannerState = row.spBanner.bannerState === 0 ? 1 : 0;
      });
    },
  }
};


</script>
